const express = require('express');
const router = express.Router();
const moment = require('moment');
const { asyncHandle } = require('../getSendResult');
const {
    manageTodayTasks,
    setManageTodayTasksisDelete,
    setManageTodayTasksisDone,
    deleteManageTodayTasks,
    addManageTodayTasks,
    updateManageTodayTasks,
    findHistoricalMission,
    findHistoricalMissionTotal,
    deleteHistoricalMissionArr,
} = require('../../services/background-task');

// 查询今日任务
router.get('/findManageTodayTasks', asyncHandle(
    async (req, res, next) => {
        const page = req.query.page || 1;
        const limit = req.query.limit || 100;
        const userId = req.query.userId;
        let result = await manageTodayTasks(page, limit, userId);
        return result.filter((item) => {
            return moment(item.creatiedTime).isSame(new Date(), 'day');
        });
    }
));

// 设置是否可删除
router.post('/setManageTodayTasksisDelete', asyncHandle(
    async (req, res, next) => {
        const id = req.body.id;
        const isDelete = req.body.isDelete;
        return await setManageTodayTasksisDelete(id, isDelete);
    }
))

// 设置是否已完成
router.post('/setManageTodayTasksisDone', asyncHandle(
    async (req, res, next) => {
        const id = req.body.id;
        const isDone = req.body.isDone;
        return await setManageTodayTasksisDone(id, isDone);
    }
))

// 删除今日任务
router.get('/deleteManageTodayTasks', asyncHandle(
    async (req, res, next) => {
        const id = req.query.id;
        return await deleteManageTodayTasks(id);
    }
))

// 布置任务
router.post('/addManageTodayTasks', asyncHandle(
    async (req, res, next) => {
        const name = req.body.name;
        const description = req.body.description;
        const content = req.body.content;
        const completeTargetTime = req.body.completeTargetTime;
        const userId = req.body.userId;
        return await addManageTodayTasks(
            name,
            description,
            content,
            completeTargetTime,
            null,
            0,
            userId
        );
    }
))

// 更新任务
router.post('/updateManageTodayTasks', asyncHandle(
    async (req, res, next) => {
        const id = req.body.id;
        const name = req.body.name;
        const description = req.body.description;
        const content = req.body.content;
        const completeTargetTime = req.body.completeTargetTime;
        return await updateManageTodayTasks(id, name, description, content, completeTargetTime);
    }
))


//-----------------------------------------------------下面需要重构

// 查询历史任务
router.post('/findHistoricalMission', asyncHandle(
    async (req, res, next) => {
        const page = req.body.page || 1;
        const limit = req.body.limit || 100;
        const search = req.body.search || '';
        const userId = req.body.userId;
        return {
            total: (await findHistoricalMissionTotal(search, userId)).length,
            data: await findHistoricalMission(page, limit, search, userId)
        }
    }
))



// 删除多条任务
router.post('/deleteHistoricalMissionArr', asyncHandle(
    async (req, res, next) => {
        const userId = req.body.userId;
        const taskArr = req.body.taskArr;
        return await deleteHistoricalMissionArr(userId, taskArr);
    }
))


module.exports = router;